package com.runa.monitor.platinterface.extend.nb.controller;

import com.alibaba.fastjson.JSONObject;
import com.runa.monitor.comm.pojo.Result;
import com.runa.monitor.platinterface.extend.nb.config.*;
import com.runa.monitor.platinterface.extend.nb.service.NbPlantService;
import com.runa.monitor.platinterface.extend.nb.vo.NewReportDataBaseVo;
import com.runa.monitor.platinterface.extend.nb.vo.ReportDataBaseVo;
import com.runa.monitor.platinterface.extend.nb.vo.lkb.DataReportDTO;
import com.runa.monitor.platinterface.platService.InterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.concurrent.Executor;

@RestController
@RequestMapping("comm")
@Slf4j
public class NbPlantfromController {

    @Resource
    private NbPlantService nbPlantServicel;

    @Resource
    private NewMqttNbIotConfig newMqttNbIotConfig;

    @Resource
    private MqttNbIotConfig mqttNbIotConfig;

    @Resource
    private XHANorthNbIotConfig northNbIotConfig;

    @Resource(name = "asyncRequestScheduler")
    private Executor executor;

    /**
     * 电信平台命令下发   此处外部出现这三个接口 唤醒周期和补偿值  阀开度 测试接口，走server
     */
    @PostMapping("/ToDxCmd")
    public Result ToDxCmd(String identity, Integer wakeUpCycle, String compensation, BigDecimal sendOpening) {
        log.info("电信平台命令下发->:" + identity + "---" + wakeUpCycle + "---" + compensation + "---" + sendOpening);
        executor.execute(() -> nbPlantServicel.ToDxCmd(identity, wakeUpCycle, compensation, sendOpening));
        return Result.success();
    }


    @PostMapping("messageReceiver")
    public String receive(@RequestBody String body) throws Exception {
        log.info("NB messageReceiver PushMessageReceiver->body:{}", body);
        if (northNbIotConfig.getEnable()) {
            executor.execute(() ->
                    nbPlantServicel.handleruna(body));
        }
        return "ok";
    }


    /**
     * 浚县-面板数据对接
     */
    @PostMapping("/receiveData")
    public String receiveData(@RequestBody String body) {
        log.info("NB数据---浚县receiveDataForjunCounty:" + body);
        return nbPlantServicel.receiveDataForjunCounty(body);
    }

    /**
     * 接收其他山东厂家的NB面板数据
     */
    @PostMapping("/lintu/messageReceiver")
    public String shandongreceive(@RequestBody String body) {
        log.info("NB数据---lintu PushMessageReceiver->body:{}:" + body);
        nbPlantServicel.receiveDataForShandongreceive(body);
        return "ok";
    }

    @PostMapping("/lintu/messageReceiver2")
    public String messageReceiver2(@RequestBody String body) {
        log.debug("lintu2 PushMessageReceiver->body:{}", body);
        nbPlantServicel.handlelingTu2(body);
        return "ok";
    }


    /**
     * !!!电信平台 消息订阅url接收小瑞面板数据
     */
    @PostMapping("receiveAEP_Panel")
    public String receiveAEP_Panel(@RequestBody String body) throws Exception {
        log.info("NB receiveAEP_Panel->body:{}", body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            log.info("小瑞面板---收到租户id：" + tenantId + ",NB-IoT receiveAEP_Panel设备上报的数据：" + body);
            if (newMqttNbIotConfig.getTenantId().equals(tenantId)) {
                NewReportDataBaseVo reportBaseDTO = JSONObject.parseObject(body, NewReportDataBaseVo.class);
                log.info("小瑞面板---收到租户id：" + tenantId + ",NB-IoT receiveAEP_Panel设备上报的解析后数据：" + JSONObject.toJSONString(reportBaseDTO) + "------getDeviceId: " + reportBaseDTO.getDeviceId());
                executor.execute(() -> nbPlantServicel.receiveAEP_Panel(reportBaseDTO));
//                handlerNBDataScheduler.execute(new NewMqttUpDataQueue(NewMqttSetUtils.APP_KEY, NewMqttSetUtils.APP_SECRET, mqttService, reportBaseDTO));
            }
        }
        return "ok";
    }

    /*
     *
     */
    @Test
    public void test() throws Exception {
        receiveAEP_Panel("{\"upPacketSN\":-1,\"upDataSN\":-1,\"topic\":\"v1/up/ad19\",\"timestamp\":1695807351441,\"tenantId\":\"2000014601\",\"serviceId\":8,\"protocol\":\"lwm2m\",\"productId\":\"15097579\",\"payload\":{\"up_time\":1695807347,\"up_data_interval\":60,\"temp_offset\":0,\"temp\":\"242\",\"start_time\":1641261664,\"soft_version\":\"V2.0.0\",\"signal_level\":5,\"save_data_interval\":60,\"lessee_code\":0,\"humidity_offset\":0,\"humidity\":\"720\",\"errcode\":0,\"device_code\":\"RN01210401DZM\",\"battery_level\":5},\"messageType\":\"dataReport\",\"deviceType\":\"\",\"deviceId\":\"c1944fbd0b0d44c5b2df91630a71d534\",\"assocAssetId\":\"\",\"IMSI\":\"undefined\",\"IMEI\":\"869401043578327\"}");
    }

    /**
     * RU面板数据接入
     *
     * @param body
     * @return
     */
    @PostMapping("receiveAEP_RU_Temperature")
    public String receiveAEP_RU_Temperature(@RequestBody String body) {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_RU_Temperature 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            if (mqttNbIotConfig.getTenantId().equals(tenantId)) {
                executor.execute(() -> nbPlantServicel.receiveAEP_RU_Temperature(baseVo));
            }
        }
        return "ok";
    }


    /**
     * 普通面板
     *
     * @param body
     * @return
     */
    @PostMapping("receiveAEP_Temperature")
    public String receiveAEP_Temperature(@RequestBody String body) {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_Temperature 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            executor.execute(() -> nbPlantServicel.receiveAEP_Temperature(baseVo, "RUNA"));
        }
        return "ok";
    }

    @PostMapping("receiveAEP_HDRX_Temperature")
    public String receiveAEP_TemperatureHdrx(@RequestBody String body) {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_Temperature 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            executor.execute(() -> nbPlantServicel.receiveAEP_Temperature(baseVo, "HDRX"));
        }
        return "ok";
    }


    /**
     * 阀数据接入
     *
     * @param body
     * @return
     */
    @PostMapping("receiveAEP_BuildValve")
    public String receiveAEP_BuildValve(@RequestBody String body) {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_Valve 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            if (mqttNbIotConfig.getTenantId().equals(tenantId) || TaiYuanCommConfig.getTyTenantId().equals(tenantId)) {
                executor.execute(() -> nbPlantServicel.receiveAEP_BuildValve(baseVo));
            }
        }
        return "ok";
    }

    /**
     * 温压数据接入
     *
     * @param body
     * @return
     * @throws Exception
     */
    @PostMapping("receiveAEP_Temperaturepressure")
    public String receiveAEP_Temperaturepressure(@RequestBody String body) throws Exception {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_Temperaturepressure 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            if (mqttNbIotConfig.getTenantId().equals(tenantId)) {
                executor.execute(() -> nbPlantServicel.receiveAEP_Temperaturepressure(baseVo));
            }
        }
        return "ok";
    }


    @PostMapping("receiveRuna_Temperaturepressure")
    public String receiveRuna_Temperaturepressure(@RequestBody String body) throws Exception {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_Temperaturepressure 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            if (mqttNbIotConfig.getTenantId().equals(tenantId)) {
                executor.execute(() -> nbPlantServicel.receiveRuna_TemperaturePressure(baseVo));
            }
        }
        return "ok";
    }


    /**
     * 楼表
     *
     * @param body
     * @return
     * @throws Exception
     */
    @PostMapping("receiveAEP_BuildMeter")
    public String receiveAEP_BuildMeter(@RequestBody String body) throws Exception {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_BuildMeter 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            if (mqttNbIotConfig.getTenantId().equals(tenantId)) {
                executor.execute(() -> nbPlantServicel.receiveAEP_BuildMeter(baseVo));
            }
        }
        return "ok";
    }

    @Resource
    private LKBMqttNbIotConfig lkbMqttNbIotConfig;

    @PostMapping("receiveAEP_LKB_Temperature")
    public String receiveAEP_LKB_Temperature(@RequestBody String body) {
        DataReportDTO baseVo = JSONObject.parseObject(body, DataReportDTO.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_LKB_Temperature 设备上报的数据：" + body);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_LKB_Temperature 设备上报的数据 上传数据解码后的内容为：" + baseVo + "------getDeviceId: " + baseVo.getDeviceId());
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
            if (lkbMqttNbIotConfig.getTenantId().equals(tenantId)) {
                executor.execute(() -> nbPlantServicel.receiveAEP_LKB_Temperature(baseVo));
            }
        }
        return "ok";
    }


    /**
     * 模拟数据
     * ZnVuYzp1cCxyc3JwOi0xMjMuMCxlY2w6MixzbnI6Ny4wLGNlbGxpZDoyMzIzNzU4OTgsdHlwOjEsaWQ6ODAwMDAwNTQsaWNjOjg5ODYxMTIxMjIyMDEzMzk3MjMwLGNzcTowMCxzZXQ6MjUwLGN1cjoyNTAuMCxyY250OjAsdDE6WzI0LjZdLHQyOlsyOS40XSxwMTpbMC4wMDBdLHAyOlswLjAwMF0sc3RhOjg5ODAscGFyOjAwMDAsdmVyOjMwNDcsbW9kZToxLHRhYzowLHJ0YzoyMDIyMDYwMTEzMjgwMixjaXR2MTo1LHVpdHYxOjcyMCxjaXR2Mjo1LHVpdHYyOjcyMCxob3Q6MTAxNTA0MDEsYmF0OjMuNjQwLGxpZmU6MjAxLHJ4dzoxMCx0MWM6MC4wLHQyYzowLjAscDFjOjAuMCxwMmM6MC4wLGQxazo1LGFjbHM6MCxhY2x0OjEwMDgwLG5iZGx5OjEsYmxvY2s6MCxyZXA6NCxtb3RvczowLG9sZHM6MkUsbmJtb2Q6QkMyOEZGSFIwMUEwMixuYnJlczo0LHJlc2V0OjI=
     * func:up,rsrp:-123.0,ecl:2,snr:7.0,cellid:232375898,typ:1,id:80000054,icc:89861121222013397230,csq:00,set:250,cur:250.0,rcnt:0,t1:[24.6],t2:[29.4],p1:[0.000],p2:[0.000],sta:8980,par:0000,ver:3047,mode:1,tac:0,rtc:20220601132802,citv1:5,uitv1:720,citv2:5,uitv2:720,hot:10150401,bat:3.640,life:201,rxw:10,t1c:0.0,t2c:0.0,p1c:0.0,p2c:0.0,d1k:5,acls:0,aclt:10080,nbdly:1,block:0,rep:4,motos:0,olds:2E,nbmod:BC28FFHR01A02,nbres:4,reset:2
     */
    /**
     * 楼阀变温压数据接入
     *
     * @param body
     * @return
     * @throws Exception
     */
    @PostMapping("receiveAEP_BuildValve_Te")
    public String receiveAEP_BuildValve_Te(@RequestBody String body) throws Exception {
        ReportDataBaseVo baseVo = JSONObject.parseObject(body, ReportDataBaseVo.class);
        log.info("---收到租户id：" + baseVo.getTenantId() + ",NB-IoT receiveAEP_BuildValve_Te 设备上报的数据：" + body);
        JSONObject jsonObject = JSONObject.parseObject(body);
        String messageType = jsonObject.getString("messageType");
        String tenantId = jsonObject.getString("tenantId");
        if ("dataReport".equals(messageType)) {
//            if (mqttNbIotConfig.getTenantId().equals(tenantId)) {
            executor.execute(() -> nbPlantServicel.receiveAEP_BuildValve_Te(baseVo));
//            }
        }
        return "ok";
    }

}
